<?php

class StatistiquesController extends Zend_Controller_Action
{

	public function init()
	{
		/* Initialize action controller here */
		include(APPLICATION_PATH."/../library/pChart/pData.class.php");
		include(APPLICATION_PATH."/../library/pChart/pDraw.class.php");
		include(APPLICATION_PATH."/../library/pChart/pImage.class.php");
		include(APPLICATION_PATH.'/../library/pChart/pPie.class.php');
		
		$this->view->addHelperPath(
		'ZendX/JQuery/View/Helper', 
		'ZendX_JQuery_View_Helper');

	}

	public function indexAction(){

	}

	public function incidentsbyreseauxAction()
	{
		$this->_helper->getHelper("layout")->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		// action body
		/* Create and populate the pData object */
		$reseaux = new Application_Model_DbTable_Reseaux();
		$tabReseaux= $reseaux->getStatsIncidentsByReseau();
		//$this->view->tabReseaux= $tabReseaux;
		$nombresIncidents=array();
		$nomsReseaux=array();
		foreach ($tabReseaux as $reseau){
			$nombresIncidents[]=$reseau["nbIncidents"];
			$nomsReseaux[]=$reseau["nomReseau"];
		}
		$MyData = new pData();
		$MyData->addPoints($nombresIncidents,"ScoreA");
		$MyData->setSerieDescription("ScoreA","Application A");

		/* Define the absissa serie */
		$MyData->addPoints($nomsReseaux,"Labels");
		$MyData->setAbscissa("Labels");

		/* Create the pChart object */
		$myPicture = new pImage(800, 520, $MyData);

		/* Draw a solid background */
		$Settings = array("R"=>170, "G"=>183, "B"=>87, "Dash"=>1, "DashR"=>190, "DashG"=>203, "DashB"=>107);
		$myPicture->drawFilledRectangle(0,0,800,520,$Settings);

		/* Overlay with a gradient */
		$Settings = array("StartR"=>219, "StartG"=>231, "StartB"=>139, "EndR"=>1, "EndG"=>138, "EndB"=>68, "Alpha"=>50);
		$myPicture->drawGradientArea(0,0,800,520,DIRECTION_VERTICAL,$Settings);
		$myPicture->drawGradientArea(0,0,800,40,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>100));

		/* Add a border to the picture */
		$myPicture->drawRectangle(0,0,799,519,array("R"=>0,"G"=>0,"B"=>0));

		/* Write the picture title */
		$myPicture->setFontProperties(array("FontName"=>APPLICATION_PATH."/../library/fonts/Silkscreen.ttf","FontSize"=>12));
		$myPicture->drawText(10,25,"Repartition des pannes par secteur (en nombre de pannes)",array("R"=>255,"G"=>255,"B"=>255));

		/* Set the default font properties */
		$myPicture->setFontProperties(array("FontName"=>APPLICATION_PATH."/../library/fonts/Forgotte.ttf","FontSize"=>15,"R"=>80,"G"=>80,"B"=>80));

		/* Enable shadow computing */
		$myPicture->setShadow(TRUE,array("X"=>2,"Y"=>2,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>50));

		/* Create the pPie object */
		$PieChart = new pPie($myPicture,$MyData);

		/* Draw an AA pie chart */
		$PieChart->draw2DPie(420,280,array("DrawLabels"=>TRUE,"LabelStacked"=>TRUE,"Border"=>TRUE,"WriteValues"=>PIE_VALUE_NATURAL,"ValuePosition"=>PIE_VALUE_INSIDE, "Radius"=>150));

		/* Write the legend box */
		$myPicture->setShadow(FALSE);
		$PieChart->drawPieLegend(15,180,array("Alpha"=>20));

		/* Render the picture (choose the best way) */
		//$myPicture->autoOutput("pictures/example.draw2DPie.labels.png");
		header('Content-type: image/png');
		$myPicture->stroke();

	}

	public function  currentetatAction(){
		$this->_helper->getHelper("layout")->disableLayout();
		$this->_helper->viewRenderer->setNoRender();

		$materiels = new Application_Model_DbTable_Materiels();
		$nbFailMateriels= $materiels->nbFailureMateriel();
		$nbTotalMateriels= $materiels ->nbTotalMateriel();

		$pct=100-floor(($nbFailMateriels/$nbTotalMateriels)*100);
		//$nbFailMateriels=intval($nbFailMateriels['nbMateriels']);
		//var_dump ($nbFailMateriels['nbMateriels']);
		$myPicture = new pImage(202,22);

		/* Set the font & shadow options */
		$myPicture->setFontProperties(array("FontName"=>APPLICATION_PATH."/../library/fonts/Forgotte.ttf", "FontSize"=>10));
		$myPicture->setShadow(TRUE,array("X"=>1, "Y"=>1, "R"=>0, "G"=>0, "B"=>0, "Alpha"=>20));




		/* Draw a progress bar */
		$progressOptions =array("R"=>255, "G"=>0, "B"=>0, "Surrounding"=>20, "BoxBorderR"=>0, "BoxBorderG"=>0, "BoxBorderB"=>0, "BoxBackR"=>255, "BoxBackG"=>255, "BoxBackB"=>255, "RFade"=>51, "GFade"=>255, "BFade"=>51, "ShowLabel"=>TRUE,"LabelPos"=>LABEL_POS_CENTER);
		$myPicture->drawProgress(0,0,$pct,$progressOptions);


		/* Render the picture (choose the best way) */
		header('Content-type: image/png');
		$myPicture->stroke();
	}

	public function historyincidentAction(){

		$this->_helper->getHelper("layout")->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$MyData = new pData();
		$incidents= new Application_Model_DbTable_Incidents();
		$tbIncidents = $incidents->statByMonthIncident(2012);
		$tbMois=array("Janvier","Fevrier","Mars","Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Decembre");
		foreach ($tbIncidents as $incidents){
			$resultTb= array();
			foreach ($tbMois as $mois){
				$resultTb[]= intval($incidents[$mois]);
			}
			$MyData->addPoints($resultTb,$incidents['Reseau']);

		}

		$MyData->setAxisName(1,"Hits");
		$MyData->addPoints($tbMois,"Months");
		$MyData->setSerieDescription("Months","Month");
		$MyData->setAbscissa("Months");



		/* Create the pChart object */
		$myPicture = new pImage(800,400,$MyData);

		/* Turn of Antialiasing */
		$myPicture->Antialias = FALSE;

		/* Add a border to the picture */
		$myPicture->drawGradientArea(0,0,800,400,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>100));
		$myPicture->drawGradientArea(0,0,800,400,DIRECTION_HORIZONTAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>20));
		$myPicture->drawRectangle(0,0,799,399,array("R"=>0,"G"=>0,"B"=>0));
		$myPicture->drawGradientArea(0,0,799,40,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>100));
		/* Set the default font */
		$myPicture->setFontProperties(array("FontName"=>APPLICATION_PATH."/../library/fonts/pf_arma_five.ttf","FontSize"=>6));
		/* Write the picture title */
		$myPicture->setFontProperties(array("FontName"=>APPLICATION_PATH."/../library/fonts/Silkscreen.ttf","FontSize"=>6));
		$myPicture->drawText(10,13,"Histogramme des incidents par Reseau",array("R"=>255,"G"=>255,"B"=>255,"FontSize"=>12));
		/* Define the chart area */
		$myPicture->setGraphArea(30,100,750,370);

		/* Draw the scale */
		$scaleSettings = array("GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>TRUE,"CycleBackground"=>TRUE);
		$myPicture->drawScale($scaleSettings);

		/* Write the chart legend */
		$myPicture->drawLegend(300,45,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));

		/* Turn on shadow computing */
		$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));

		/* Draw the chart */
		$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));
		$settings = array("Surrounding"=>-30,"InnerSurrounding"=>30,"DisplayValues"=>TRUE);
		$myPicture->drawBarChart($settings);

		/* Render the picture (choose the best way) */
		header('Content-type: image/png');
		$myPicture->stroke();
	}

	public function incidentsbyemployesAction(){
		$this->_helper->getHelper("layout")->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$MyData = new pData();
		$incidents= new Application_Model_DbTable_Incidents();
		$tbIncidents = $incidents->statByEmployeIncident();
		//var_dump($tbIncidents);
		$tbNbIncidents= array();
		$tbNbEmployes = array();
		foreach ($tbIncidents as $incidents){
			$tbNbIncidents[] = $incidents['nbIncidents'];
			$tbNbEmployes[] = $incidents['nomEmploye']." ".$incidents['prenomEmploye'];
		}
		$MyData->addPoints($tbNbIncidents,"Hits");
		$MyData->setAxisName(0,"Nombre d'incidents");
		$MyData->addPoints($tbNbEmployes,"Browsers");
		$MyData->setSerieDescription("Browsers","Browsers");
		$MyData->setAbscissa("Browsers");
		$MyData->setAbscissaName("Employes");


		/* Create the pChart object */
		$myPicture = new pImage(800, 500, $MyData);
		$myPicture->drawGradientArea(0,0,800,500,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>100));
		$myPicture->drawGradientArea(0,0,800,500,DIRECTION_HORIZONTAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>20));
		$myPicture->drawGradientArea(0,0,800,40,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>100));
		$myPicture->setFontProperties(array("FontName"=>APPLICATION_PATH."/../library/fonts/pf_arma_five.ttf","FontSize"=>6));
		$myPicture->drawText(10,13,"Nombre d'incidents par employe",array("R"=>255,"G"=>255,"B"=>255,"FontSize"=>12));
		//$myPicture->drawScale(array("YMargin"=>1));
		/* Draw the chart scale */
		$myPicture->setGraphArea(100,70,760,480);
		$myPicture->drawScale(array("CycleBackground"=>TRUE,"DrawSubTicks"=>TRUE,"GridR"=>0,"GridG"=>0,"GridB"=>0,"GridAlpha"=>10,"Pos"=>SCALE_POS_TOPBOTTOM)); //

		/* Turn on shadow computing */
		$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));

		/* Draw the chart */
		$myPicture->drawBarChart(array("DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"Rounded"=>TRUE,"Surrounding"=>30));

		/* Write the legend */
		$myPicture->drawLegend(800,215,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));

		/* Render the picture (choose the best way) */
		header('Content-type: image/png');
		$myPicture->stroke();
	}

	public function coutcompareAction(){

		$this->_helper->getHelper("layout")->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$MyData = new pData();
		$materiels = new Application_Model_DbTable_Materiels();
		$materielsTb= $materiels->statCompareCoutMateriel();
		$prixMaterielsTb= array();
		$sommeIncidentsMaterielsTb= array();
		$nomMaterielsTb= array();
		foreach ($materielsTb as $materiel){
			$sommeIncidentsMaterielsTb[]=$materiel['sommeIncidents'];
			$prixMaterielsTb[] = $materiel['prixPoste'];
			$nomMaterielsTb[] = $materiel ['raccourciReseau']."".$materiel['numeroPoste'];
		}
		$MyData->addPoints($prixMaterielsTb,"Prix du materiel");
		$MyData->addPoints($sommeIncidentsMaterielsTb,"Cout total des incidents");
		$MyData->setAxisName(0,"euros");
		$MyData->addPoints($nomMaterielsTb,"Materiels");

		$MyData->setSerieDescription("Materiels","Materiel");
		$MyData->setAbscissa("Materiels");



		/* Create the pChart object */
		$myPicture = new pImage(800, 400, $MyData);
		
		$myPicture->drawGradientArea(0,0,800,400,DIRECTION_VERTICAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>100));
		$myPicture->drawGradientArea(0,0,800,400,DIRECTION_HORIZONTAL,array("StartR"=>240,"StartG"=>240,"StartB"=>240,"EndR"=>180,"EndG"=>180,"EndB"=>180,"Alpha"=>20));
		$myPicture->drawGradientArea(0,0,800,40,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>100));
		$myPicture->setFontProperties(array("FontName"=>APPLICATION_PATH."/../library/fonts/pf_arma_five.ttf","FontSize"=>9));
		$myPicture->drawText(10,13,"Comparaison des couts d'achat et de reparation des materiels",array("R"=>255,"G"=>255,"B"=>255,"FontSize"=>12));
		/* Draw the scale  */
		$myPicture->setGraphArea(80,80,740,370);
		$myPicture->drawScale(array("CycleBackground"=>TRUE,"DrawSubTicks"=>TRUE,"GridR"=>0,"GridG"=>0,"GridB"=>0,"GridAlpha"=>10));

		/* Turn on shadow computing */
		$myPicture->setShadow(TRUE,array("X"=>1,"Y"=>1,"R"=>0,"G"=>0,"B"=>0,"Alpha"=>10));

		/* Draw the chart */
		$settings = array("Gradient"=>TRUE,"GradientMode"=>GRADIENT_EFFECT_CAN,"DisplayPos"=>LABEL_POS_INSIDE,"DisplayValues"=>TRUE,"DisplayR"=>255,"DisplayG"=>255,"DisplayB"=>255,"DisplayShadow"=>TRUE,"Surrounding"=>10);
		$myPicture->drawBarChart($settings);

		/* Write the chart legend */
		$myPicture->drawLegend(800,55,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));


		/* Render the picture (choose the best way) */
		header('Content-type: image/png');
		$myPicture->stroke();
	}

	public function testsqlAction(){

		$materiels = new Application_Model_DbTable_Materiels();
		$materielsTb= $materiels->statCompareCoutMateriel();
		$prixMaterielsTb= array();
		$sommeIncidentsMaterielsTb= array();
		$nomMaterielsTb= array();
		foreach ($materielsTb as $materiel){
			$sommeIncidentsMaterielsTb[]=$materiel['sommeIncidents'];
			$prixMaterielsTb[] = $materiel['prixPoste'];
			$nomMaterielsTb[] = $materiel ['raccourciReseau']."".$materiel['numeroPoste'];
		}
		var_dump($nomMaterielsTb);
		var_dump($prixMaterielsTb);
		var_dump($sommeIncidentsMaterielsTb);

	}



}

